package whisper.service.repository.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import whisper.service.domain.db.Task;
import whisper.service.repository.TaskRepository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by JT on 2017/11/19.
 */
@Repository
public class TaskRepositoryImpl implements TaskRepository {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private static final String TABLE_NAME_WM_TASK = "WM_TASK";
    private static final String TABLE_NAME_WM_SOURCE = "WM_SOURCE";
    private static final String TABLE_NAME_WM_TARGET = "WM_TARGET";

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public void addTask(Task task) {
        String sql = String.format("INSERT INTO %s(SOURCEID, TARGETID, TASKREGISTERTIME, TASKSTATUS) " +
                        "VALUES(:SOURCEID, :TARGETID, :TASKREGISTERTIME, :TASKSTATUS)",
                TABLE_NAME_WM_TASK);
        Map<String, Object> params = new HashMap<>();
        params.put("SOURCEID", task.getSourceId());
        params.put("TARGETID", task.getTargetId());
        params.put("TASKREGISTERTIME", task.getTaskRegisterTime());
        params.put("TASKSTATUS", task.getTaskStatus());
        jdbcTemplate.update(sql, params);
    }

    @Override
    public List<Task> findTasksByStatus(String status) {
        String sql = String.format("SELECT taskId AS TASKID, T.targetId AS TARGETID, sourcePath AS SOURCEPATH, targetPath AS TARGETPATH,  taskStatus AS TASKSTATUS" +
                " FROM %s as K, %s as S, %s as T WHERE K.sourceId = S.sourceId AND K.targetId = T.targetId AND TASKSTATUS = :TASKSTATUS",
                TABLE_NAME_WM_TASK, TABLE_NAME_WM_SOURCE, TABLE_NAME_WM_TARGET);
        Map<String, Object> params = new HashMap<>();
        params.put("TASKSTATUS", status);
        return jdbcTemplate.query(sql, params, new BeanPropertyRowMapper<>(Task.class));
    }

    @Override
    public List<Task> findTasksByStatusList(List<String> status) {
        String sql = String.format("SELECT taskId AS TASKID, T.targetId AS TARGETID, sourcePath AS SOURCEPATH, targetPath AS TARGETPATH,  taskStatus AS TASKSTATUS" +
                        " FROM %s as K, %s as S, %s as T WHERE K.sourceId = S.sourceId AND K.targetId = T.targetId AND TASKSTATUS IN (:TASKSTATUS)",
                TABLE_NAME_WM_TASK, TABLE_NAME_WM_SOURCE, TABLE_NAME_WM_TARGET);
        Map<String, Object> params = new HashMap<>();
        params.put("TASKSTATUS", status);
        return jdbcTemplate.query(sql, params, new BeanPropertyRowMapper<>(Task.class));
    }

    @Override
    public void updateTaskById(Task task) {
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append(" UPDATE ").append(TABLE_NAME_WM_TASK)
                .append(" SET ").append(" taskId = ").append(task.getTaskId());

        /* 拼接set */
        sqlBuilder = task.getSourceId() != 0 ?
                sqlBuilder.append(" , sourceId = ").append(task.getSourceId()) : sqlBuilder;
        sqlBuilder = task.getTargetId() != 0 ?
                sqlBuilder.append(", targetId = ").append(task.getTargetId()) : sqlBuilder;
        sqlBuilder = StringUtils.isNotBlank(task.getTaskRegisterTime()) ?
                sqlBuilder.append(" , taskRegisterTime = '").append(task.getTaskRegisterTime()).append("'") : sqlBuilder;
        sqlBuilder = StringUtils.isNoneBlank(task.getTaskStartTime()) ?
                sqlBuilder.append(", taskStartTime = '").append(task.getTaskStartTime()).append("'") : sqlBuilder;
        sqlBuilder = StringUtils.isNotBlank(task.getTaskFinishTime()) ?
                sqlBuilder.append(" , taskFinishTime = '").append(task.getTaskFinishTime()).append("'") : sqlBuilder;
        sqlBuilder = task.getTaskStatus() != 0 ?
                sqlBuilder.append(", taskStatus = ").append(task.getTaskStatus()) : sqlBuilder;

        sqlBuilder.append(" WHERE taskId = :taskId");

        Map<String, String> params = new HashMap(1);
        params.put("taskId", task.getTaskId().toString());
        jdbcTemplate.update(sqlBuilder.toString(), params);

        logger.debug("Running SQL: {}", sqlBuilder.toString());
    }
}
